package com.ztc.ZtcCorporation.web;
import java.util.HashMap;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ztc.ZtcCorporation.bean.ZtcCorporation;
import com.ztc.ZtcCorporation.service.ZtcCorporationManager;
@Controller
@RequestMapping("ztc_corporation_action")
public class ZtcCorporationAction {
    @Autowired
    private ZtcCorporationManager  ztcCorporationManager;
    private static final Logger log = LogManager.getLogger(ZtcCorporation.class);
    @RequestMapping("sign")
	@ResponseBody
	public HashMap<String, Object> save(String name, String address, String phone, String type, String areaCode, String userId) {
		log.info("开始添加");
		HashMap<String, Object> wapper = new HashMap<>();
		ZtcCorporation ztcCorporation = generateCorporation(name, address, phone, type, areaCode, userId);
		String id = ztcCorporationManager.save(ztcCorporation);
		if (id == null){
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("data", "重复添加");
			log.info("重复添加");
			return wapper;
		}else{
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("data", "添加成功");
			log.info("添加成功");
			return wapper;
		}
	}

	private ZtcCorporation generateCorporation(String name, String address, String phone, String type, String areaCode,
			String userId) {
		ZtcCorporation ztcCorporation=new ZtcCorporation();
		ztcCorporation.setName(name);
		ztcCorporation.setAddress(address);
		ztcCorporation.setPhone(phone);
		if(type!=null){
			ztcCorporation.setType(Integer.parseInt(type));
		}
		ztcCorporation.setAreaCode(areaCode);
		ztcCorporation.setUserId(userId);
		return ztcCorporation;
	}

	@RequestMapping("init")
	@ResponseBody
	public HashMap<String, Object> getAll() {
		List<ZtcCorporation> lists = ztcCorporationManager.getAll();
		HashMap<String, Object> wapper = new HashMap<>();
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", lists);
		return wapper;
	}

	@RequestMapping("delete")
	@ResponseBody
	public HashMap<String, Object> delete(String id) {
		String result = ztcCorporationManager.delete(id);
		HashMap<String, Object> wapper = new HashMap<>();
		if ("1".equals(result)) {
			wapper.put("success", Boolean.valueOf(true));
			wapper.put("data", "删除成功");
		} else {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("data", "操作失败");
		}
		return wapper;
	}

	@RequestMapping("searchInfoEquals")
	@ResponseBody
	public HashMap<String, Object> searchInfoEquals(String name, String address, String phone, String type, String areaCode, String userId) {
		ZtcCorporation ztcCorporation=generateCorporation(name, address, phone, type, areaCode, userId);
		List<ZtcCorporation> lists = ztcCorporationManager.searchInfoEquals(ztcCorporation);
		HashMap<String, Object> wapper = new HashMap<>();
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", lists);
		return wapper;
	}
	
	@RequestMapping("searchInfoLikes")
	@ResponseBody
	public HashMap<String, Object> searchInfoLikes(String name, String address, String phone, String userId) {
		ZtcCorporation ztcCorporation=new ZtcCorporation();
		ztcCorporation.setName(name);
		ztcCorporation.setAddress(address);
		ztcCorporation.setPhone(phone);
		ztcCorporation.setUserId(userId);
		List<ZtcCorporation> lists = ztcCorporationManager.searchInfoLikes(ztcCorporation);
		HashMap<String, Object> wapper = new HashMap<>();
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", lists);
		return wapper;
	}

	@RequestMapping("update")
	@ResponseBody
	public HashMap<String, Object> update(String name, String address, String phone, String type, String areaCode, String userId,String id) {
		log.info("开始修改");
		HashMap<String, Object> wapper = new HashMap<>();
		ZtcCorporation ztcCorporation=generateCorporation(name, address, phone, type, areaCode, userId);
		ztcCorporation.setId(id);
		String result=ztcCorporationManager.update(ztcCorporation);
		if("1".equals(result)){
			wapper.put("success", Boolean.valueOf(true));
			wapper.put("data", "修改成功");
			log.info("修改成功");
		}else{
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("data", "修改失败");
			log.info("修改失败");
		}
		return wapper;
	}
}